<template>
  <div class="edit_name">
    <van-nav-bar
      title="昵称"
      left-text="返回"
      right-text="完成"
      left-arrow
      @click-left="handleCompleteClick"
      @click-right="handleCompleteClick"
    />
    <div class="edit_field">
      <van-field
        v-model="message"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入留言"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from "@/request/home";
import { Toast } from "vant";

export default {
  props: {
    name: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      message: this.name
    };
  },
  methods: {
    async handleCompleteClick() {
      Toast.loading({
        message: "保存中...",
        forbidClick: true
      });
      try {
        await updateUserProfile({
          name: this.message
        });
        Toast.success({
          message: "保存成功"
        });
        this.$emit("close");
      } catch (error) {
        if (error && error.response && error.response.status === 429) {
          Toast.fail({
            message: "昵称已存在"
          });
        }
      }
    }
  }
};
</script>
<style lang="less" scoped>
.edit_name {
  // background-color: red;
  .edit_field {
    padding: 10px 10px;
  }
}
</style>